Welcome back. In this question,
we're asked to create a dictionary called D that keeps
track of all of the characters in the string placement,
and notes how many times each character was seen.
So, in order to do this,
we're going to kind of use the standard dictionary accumulation pattern,
but this isn't all that this question asks us to do.
It says then, find the key with the lowest value in the dictionary,
and assign that key to be min value.
In order to do this part of the question,
then we're going to use the standard accumulation pattern,
more specifically actually, we're going to use min or max value accumulation.
So, let's do this first part which involves dictionary accumulation first.
So, I'm going to create a dictionary D,
initialize it to an empty dictionary,
and I'll loop through every character in placement,
so I'll say for every character C in placement.
If that character is not in our dictionary,
so if C is not in D,
then initialize D sub C to be zero,
and then regardless we're going to say D sub C equals its previous value, plus one.
Now by the time we get to line nine here,
we've accomplished this first part of the question,
so we have a dictionary D,
where every character is associated with
the number of times that that character appears in placement.
Now let's do the second part where we want to find
the key with the lowest value in this dictionary,
and assign that key to be min value.
So, the first thing that I'm going to do is
I'm actually just going to get a list of keys,
so I'm going to say, keys equals list of D.keys.
So, keys is going to be a list and every item in that list is going to be a character,
so we might have one item is the character A,
one item has the character E,
one item has the character C,
one might be a space, and so on.
So, Keys is a list of the characters in placement,
or a list of all of the keys in our dictionary D,
and now we want to get the key that has the lowest value in our dictionary D. So,
we're going to use max value accumulation,
in this case we actually are searching for the minimum value,
but it's the same concept.
So, I'm going to initialize min value to be keys sub zero,
I'm just going to do this arbitrarily to give us a starting point,
so in other words min value is going to start out as the first key in our list of keys,
so it might start out as something like the character A.
Then we're going to search for any key that has a lower value associated with it,
so I'll say for every key in keys,
now ask is the value associated with this key?
So, if D sub key is less than the value associated with min value,
so if D sub key is less than D sub min value,
then we say our new lowest key is this new key,
so I'll say min value equals key.
So, again we kind of have two parts in this question,
the code from line three, through eight,
addresses the first part where we create a dictionary with character frequencies,
and then lines ten, through 15,
address the second part of finding the key with the lowest value.
We use information from this first part,
in other words we use this dictionary D in the second part,
but we kind of apply the accumulation pattern two different times to solve this problem.
So, when I save and run my code,
you can see that I get the correct values.
So, in this question we're asked to create a dictionary called Let
D that keeps track of all of the characters in the string product,
and notes how many times each character was seen.
And then, we find the key with the highest value in that dictionary,
and assign that to be max value.
So, again here we have kind of two different parts,
the first part uses dictionary accumulation,
the second part uses max value accumulation.
So, to do the first part we're going to create a dictionary,
let_D, initialize it to an empty dictionary,
then we're going to loop through every character in product, and we're going to say,
if that character is not in let D,
then let D sub C is initialized to zero,
then we say let D sub C equals its previous value, plus one.
So, if I print out the value of let D at this point,
then I should get a dictionary where keys are
associated with the number of times that that character appears.
Now, we want to find the key with the highest value in this dictionary.
So, in this case, it's going to be N,
so I'm going to get a list of keys I'll say keys equals list of let D.keys,
and for every key in keys,
actually first I'm going to say max value equals the first key,
and for every key in keys,
if the value associated with that key,
so if let D sub key is greater than let D sub max value,
then I've seen a new largest value,
so I'll say max value equals key.
Now, when I run this code,
and I should see that I pass every test in max value ends up being N,
which was the correct answer.
That's all for now until next time.